package com.digitalchina.lxnsy.api;

import android.app.Activity;
import android.os.Handler;
import android.text.TextUtils;

import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.xml.UserXML;
import com.lzy.okhttputils.model.HttpParams;

import java.io.File;
import java.util.List;

import top.zibin.luban.CompressionPredicate;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.api
 * 创建人：白彦东
 * 创建时间：2016/10/20 10:12
 * 修改备注：
 * ================================================
 */

public class BusinessFile {

    /**
     * 单文件上传
     *
     * @param activity
     * @param filePath
     * @param handler
     */
    public static void upload(Activity activity, String filePath, Handler handler) {

//        LogUtils.e(filePath);
//        LogUtils.e(new File(filePath));
        HttpParams params = new HttpParams();
        params.put("file", new File(filePath));
        params.put("loginName", UserXML.getLoginName(activity));
        params.put("isWaterMar", "0");

        OkHttpUtil.post(activity, URL.URL_FILE_UPLOAD, "", params, handler, MSG.MSG_FILE_UPLOAD_SUCCESS, MSG.MSG_FILE_UPLOAD_FIELD);

    }

    /**
     * 多文件上传
     *
     * @param activity
     * @param handler
     * @param fileList
     */
    public static void uploads(Activity activity,
                               Handler handler, List<File> fileList) {
        HttpParams params = new HttpParams();
        for (int i = 0; i < fileList.size(); i++) {
            params.put("files" + "[" + i + "]", fileList.get(i));
        }
        params.put("loginName", UserXML.getLoginName(activity));
        params.put("description", "");
        params.put("isWaterMar", "0");

        OkHttpUtil.post(activity, URL.URL_FILE_UPLOAD, "", params, handler, MSG.MSG_FILE_UPLOAD_SUCCESS, MSG.MSG_FILE_UPLOAD_FIELD);

    }






    /**
     * 图片压缩
     *
     * @param activity
     * @param filePath
     * @param handler
     */
    public static void imageCompressAndUpload(final Activity activity, final String filePath, final Handler handler) {

        Luban.with(activity)
                .load(new File(filePath))
                .ignoreBy(100)
//                .setTargetDir(getPath())
                .filter(new CompressionPredicate() {
                    @Override
                    public boolean apply(String path) {
                        return !(TextUtils.isEmpty(path) || path.toLowerCase().endsWith(".gif"));
                    }
                })
                .setCompressListener(new OnCompressListener() {
                    @Override
                    public void onStart() {
                        // TODO 压缩开始前调用，可以在方法内启动 loading UI
                    }

                    @Override
                    public void onSuccess(File file) {
                        // TODO 压缩成功后调用，返回压缩后的图片文件
                        HttpParams params = new HttpParams();
                        params.put("file", file);
                        params.put("appId", "app009");
                        params.put("isWaterMar", "0");

                        OkHttpUtil.postP(activity, URL.URL_FILE_UPLOAD, "", params, handler, MSG.MSG_FILE_UPLOAD_SUCCESS, MSG.MSG_FILE_UPLOAD_FIELD);
                    }

                    @Override
                    public void onError(Throwable e) {
                        // TODO 当压缩过程出现问题时调用
                    }
                }).launch();
    }



}
